package com.six.backend.service.impl;


import com.six.backend.dao.PermissionDao;
import com.six.backend.dao.UserDao;
import com.six.backend.pojo.Authority;
import com.six.backend.pojo.LoginLog;
import com.six.backend.pojo.User;
import com.six.backend.service.UserService;
import com.six.backend.utils.EncryptUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 张重阳
 */
@Service
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {
    @Autowired
    private final UserDao userDao;
    @Autowired
    private PermissionDao permissionDao;


    @Override
    public List<LoginLog> selectLoginLog(Map map) {
        return userDao.selectLoginLog(map);
    }

    @Override
    public User selectUserByAccount(User user) {
        User realUser = userDao.selectUserByAccount(user);
        if(realUser == null) {
            return null;
        }else {
            if (user.getPassword().equals(EncryptUtil.decrypt(realUser.getPassword()))) {
                realUser.setPassword(null);
                return realUser;
            }
            return null;
        }
    }

    @Override
    public User selectUserBYId(String account) {
        return userDao.selectUserById(account);
    }

    @Override
    public List<Map<String, Object>> getMenuList(String account) {
        List<Map<String,Object>> list = new ArrayList<>();
        List<Authority> authorityList = permissionDao.selectFatherPermissionByAccount(account);
        for (Authority authority : authorityList) {
            Map<String,Object> map = new HashMap<>();
            authority.setPermission(null);
            map.put("menu",authority);
            List<Authority> authorityList1 = permissionDao.selectSubPermissionByFatherId(authority.getId());
            for (Authority authority1 : authorityList1) {
                authority1.setPermission(null);
            }
            map.put("subMenu",authorityList1);
            list.add(map);
        }
        return list;
    }

    @Override
    public List<User> selectAllUsers() {
        return userDao.selectAllUsers();
    }

    @Override
    public Boolean insertUser(Map map) {
        if (userDao.insertUser(map) == 1) {
            return true;
        }
        return false;
    }

    @Override
    public Boolean isExist(String account) {
        User user = userDao.selectUserById(account);
        if (user == null) {
            return false;
        }else {
            return true;
        }
    }

    @Override
    public Integer insertUserRole(Map map) {
        return userDao.insertUserRole(map);
    }

    @Override
    public List<User> selectUser(Map map) {
        return userDao.selectUser(map);
    }

    @Override
    public Boolean deleteUser(String account) {
        if (userDao.deleteUser(account) == 1 && userDao.deleteUserRole(account) != null) {
            return true;
        }else {
            return false;
        }
    }

    @Override
    public Boolean deleteUserRole(String account) {
        userDao.deleteUserRole(account);
        return true;
    }

    @Override
    public void insertLoginLog(String account, String ipAddr, String format) {
        userDao.insertLoginLog(account,ipAddr,format);
    }

    @Override
    public Boolean updateUser(Map map) {
        Integer integer;
        if (isExistUserRole(map.get("account").toString())) {
            integer = userDao.updateUserRole(map);
        }else {
            integer = userDao.insertUserRole(map);

        }
        return userDao.updateUser(map) > 0 || integer > 0;
    }

    @Override
    public Boolean isExistUserRole(String account) {
        return userDao.selectUserRole(account) != null;
    }

    @Override
    public String selectUserRole(String account) {
        return userDao.selectUserRole(account).getRoleName();
    }

    @Override
    public Boolean updateStatus(User user) {
        return userDao.updateStatus(user) == 1;
    }

    @Override
    public Boolean updatePassword(Map map) {
        return userDao.updatePassword(map) == 1;
    }
}
